<template>
  <div class="ratings" ref="ratingsRef">
    <div class="ratings-wrap">
      <div class="ratings-header">
        <div class="content-left">
          <span class="score">{{seller.score}}</span>
          <span class="title">综合评分</span>
          <span class="text">高于周边商家{{seller.rankRate}}%</span>
        </div>
        <div class="content-right">
          <div class="item">
            <span class="title">服务态度</span>
            <star :size="36" :score="seller.serviceScore"></star>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="item">
            <span class="title">商品评分</span>
            <star :size="36" :score="seller.foodScore"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="item">
            <span class="title">送达时间</span>
            <span class="time">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <split-line></split-line>
      <ratings-select
        @select="select"
        @toggleContent="toggleContent"
        :ratings="ratings"
        :selectType="selectType"
        :onlyContent="onlyContent"
      ></ratings-select>
      <div class="ratings-detail">
        <ul class="ratings-wrapper">
          <li
            v-show="needShow(rating.rateType,rating.text)"
            class="rating"
            v-for="(rating,i) in ratings"
            :key="i"
          >
            <div class="avatar">
              <img :src="rating.avatar" class="img" />
            </div>
            <div class="content">
              <h1 class="username">{{rating.username}}</h1>
              <div class="star-wrap">
                <star class="score" :size="24" :score="rating.score"></star>
                <span v-if="rating.deliveryTime" class="delivery">{{rating.deliveryTime}}分钟送达</span>
              </div>
              <p class="text">{{rating.text}}</p>
              <div class="recommend" v-if="rating.recommend && rating.recommend.length > 0">
                <i
                  class="iconfont"
                  :class="{'icon-thumb_up':rating.rateType === 0,'icon-thumb_down':rating.rateType === 1}"
                ></i>
                <span v-for="(recommend,i) in rating.recommend" :key="i" class="item">{{recommend}}</span>
              </div>
              <div class="time">{{rating.rateTime | pattern("yyyy-MM-dd hh:mm")}}</div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import ratingsSelect from './RatingSelect'
import star from '../star/Star'
import splitLine from '../SplitLine'
import BScroll from 'better-scroll'

const ALL = 2
export default {
  props: ['seller'],
  data() {
    return {
      ratings: [],
      scoll: null,
      onlyContent: false,
      selectType: ALL
    }
  },
  created() {
    this.getRatingsData()
  },
  methods: {
    select(type) {
      this.selectType = type
    },
    toggleContent() {
      this.onlyContent = !this.onlyContent
    },
    needShow(type, text) {
      if (this.onlyContent && !text) {
        return false
      } else if (this.selectType === ALL) {
        return true
      } else {
        console.log(type, this.selectType)
        return this.selectType === type
      }
    },
    async getRatingsData() {
      const { data: res } = await this.$http.get('ratings')
      this.ratings = res.data
      console.log(this.ratings)
      this.$nextTick(() => {
        this.scoll = new BScroll(this.$refs.ratingsRef, {
          click: true
        })
      })
    }
  },
  components: {
    ratingsSelect,
    star,
    splitLine
  }
}
</script>
<style lang="stylus" scoped>
.ratings {
  position: absolute;
  width: 100%;
  top: 3.48rem;
  bottom: 0.92rem;
  overflow: hidden;

  .ratings-header {
    padding: 0.36rem 0;
    display: flex;

    .title {
      font-size: 0.24rem;
      color: rgb(7, 17, 27);
    }

    .content-left {
      flex: 0 0 2.75rem;
      border-right: 1px solid rgb(7, 17, 27, 0.1);
      display: flex;
      flex-direction: column;
      align-items: center;

      .score {
        font-size: 0.48rem;
        color: rgb(255, 153, 0);
        line-height: 0.56rem;
      }

      .title {
        clear: both;
      }

      .text {
        font-size: 0.2rem;
        color: rgb(147, 153, 159);
        margin-top: 0.16rem;
      }
    }

    .content-right {
      flex: 1;
      padding-left: 0.48rem;

      .item {
        margin-bottom: 0.16rem;

        .title {
          margin-right: 0.24rem;
        }

        .star {
          display: inline-block;
          vertical-align: middle;
          margin-right: 0.24rem;
        }

        .time {
          font-size: 0.24rem;
          color: rgb(147, 153, 159);
        }

        .score {
          font-size: 0.24rem;
          color: rgb(255, 153, 0);
          line-height: 0.36rem;
        }
      }
    }
  }

  .ratings-detail {
    padding: 0 0.36rem;

    .rating {
      position: relative;
      display: flex;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      padding-bottom: 0.36rem;
      margin-top: 0.36rem;

      .avatar {
        flex: 0 0 0.8rem;

        .img {
          width: 0.56rem;
          height: 0.56rem;
          border-radius: 50%;
        }
      }

      .content {
        flex: 1;

        .username {
          font-size: 0.2rem;
          color: rgb(7, 17, 27);
          line-height: 0.24rem;
        }

        .star-wrap {
          margin-top: 0.08rem;

          .score {
            display: inline-block;
            margin-right: 0.12rem;
          }

          .delivery {
            color: rgb(147, 153, 159);
            font-size: 0.2rem;
            font-weight: 200;
          }
        }

        .text {
          font-size: 0.24rem;
          margin-top: 0.12rem;
          color: rgb(7, 17, 27);
          line-height: 0.36rem;
          font-weight: 700;
        }

        .recommend {
          margin-top: 0.16rem;

          .iconfont {
            font-size: 0.24rem;
            line-height: 0.32rem;
            margin-right: 0.16rem;

            &.icon-thumb_down {
              color: rgb(183, 187, 191);
            }

            &.icon-thumb_up {
              color: rgb(0, 180, 220);
            }
          }

          .item {
            vertical-align: middle;
            display: inline-block;
            padding: 0.06rem 0.12rem;
            margin-bottom: 0.12rem;
            font-size: 0.18rem;
            margin-right: 0.16rem;
            border: 1px solid rgba(7, 17, 27, 0.1);
            border-radius: 0.02rem;
            background-color: #fff;
            overflow: hidden;
            text-overflow: hidden;
            white-space: nowrap;
          }
        }
      }

      .time {
        position: absolute;
        right: 0;
        top: 0;
        font-size: 0.2rem;
        font-weight: 200;
        color: rgb(147, 153, 159);
        line-height: 0.24rem;
      }
    }
  }
}
</style>
